import json
from urllib.request import urlopen
from pprint import pprint

# 想读写JSON(JavaScript Object Notation)编码格式的数据
def demo_1():
    data = {
        'name':'ACME',
        'shares':100,
        'price':542.23
    }
    json_str = json.dumps(data)
    print(json_str)
    data = json.loads(json_str)
    print(data)

# 读文件json数据
def demo_2():
    with open('demo_data.json', 'r') as f:
        data = json.load(f)
        print(data)

# 写文件json数据
def demo_3():
    data = {
        'name': 'kepler',
        'shares': 123,
        'price': 123.45
    }
    with open('../temp/demo_data.json', 'w') as f:
        json.dump(data, f)

def demo_4():
    u = urlopen('http://www.baidu.com')
    resp = json.loads(u.read().decode('utf-8'))
    pprint(resp)

if __name__ == '__main__':
    # demo_1()
    # demo_2()
    # demo_3()
    demo_4()